package com.demo.study.behavior.d01_chainOfResponsibility.serverlet;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Baymax
 * @since 2021/1/17
 */
public class FilterChain implements Filter {
    private List<Filter> filters = new ArrayList<>();

    private int index = 0;

    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        if (index == filters.size()) {
            return false;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(request, response, filterChain);
        return true;
    }

    public FilterChain add(Filter filter) {
        filters.add(filter);
        return this;
    }
}
